package com.bnyy.medicalHousekeeper.moudle.message.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.moudle.message.MessageManager;
import com.bnyy.medicalHousekeeper.moudle.message.adapter.VerificationMessageAdapter;
import com.bnyy.message.bean.chat.Chat;
import com.bnyy.message.bean.chat.ChatMessage;
import com.bnyy.message.bean.chat.message_data.VerificationMessage;
import com.bnyy.message.event.ReceiveMessageEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/12/1
 **/
public class VerificationMessageActivity extends BaseActivityImpl {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private VerificationMessageAdapter adapter;

    public static void show(Context context, Chat chat) {
        Intent intent = new Intent(context, VerificationMessageActivity.class);
        intent.putExtra("chat", chat);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_recycler_view;
    }

    @Override
    public String getTitleStr() {
        return "验证消息";
    }

    @Override
    public boolean registerEventBus() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Chat chat = (Chat) getIntent().getSerializableExtra("chat");
        MessageManager.clearUnreadCount(chat);

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        int padding = SizeUtils.dp2px(12);
        recyclerView.setPadding(padding, 0, padding, padding);

        adapter = new VerificationMessageAdapter(mContext);
        recyclerView.setAdapter(adapter);

        ArrayList<ChatMessage> verificationMessages = MessageManager.getVerificationMessages(chat.getChatId());
        adapter.refresh(verificationMessages);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(ReceiveMessageEvent event) {

        switch (event.getMessageType()) {
            case GROUP_CHAT_VERIFICATION:
                ChatMessage chatMessage = (ChatMessage) event.getMessage();
                VerificationMessage verificationMessage = (VerificationMessage) chatMessage.getMessageData();
                if (verificationMessage != null) {
                    ArrayList<ChatMessage> datas = adapter.getDatas();
                    for (int i = 0; i < datas.size(); i++) {
                        ChatMessage data = datas.get(i);
                        VerificationMessage message = (VerificationMessage) data.getMessageData();
                        if (message != null) {
                            if (message.getEnter_id() == verificationMessage.getEnter_id()) {
                                adapter.getDatas().remove(i);
                                break;
                            }
                        }
                    }
                    adapter.getDatas().add(0, chatMessage);
                    adapter.notifyDataSetChanged();
                }

                break;
        }
    }
}































